﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace Universum.Model.Services
{
	public static class PluralizeStringExtension{
		public static string Pluralize(this string originalString, int value)
		{
			if (value == 1) return value + originalString;
			return value + originalString + "s";
		}
	}
	public class DurationServiceImpl : IDurationService
	{
		const string MINUTE = " minute";
		const string HOUR = " hour";
		const string DAY = " day";
		const string WEEK = " week";

		#region IDurationService Members

		public string Since(DateTime dateTime)
		{
			var time_span = DateTime.Now - dateTime;


			return get_from(time_span);
		}

		#endregion
		private string get_from(TimeSpan time_span)
		{
			int weeks = get_from_weeks(time_span);
			int day = get_from_days(time_span);
			int hour = get_from_hours(time_span);
			int minute = get_from_minutes(time_span);
			 
			string result_weeks = weeks > 0 ? WEEK.Pluralize(weeks) : null;
			string result_days = day > 0 ? DAY.Pluralize(day) : null;
			string result_hours = hour > 0 ? HOUR.Pluralize(hour) : null;
			string result_minutes = minute > 0 ? MINUTE.Pluralize(minute) : "less than a minute ";
			if (result_weeks != null) return result_weeks + " and " + result_days;
			if (result_days != null) return result_days + " and "+ result_hours;
			if (result_hours != null) return result_hours + " and "+ result_minutes;
			return result_minutes;
		}

		private int get_from_weeks(TimeSpan time_span)
		{
			return  time_span.Days / 7;
		}
		private int get_from_days(TimeSpan time_span)
		{
			return time_span.Days ;
		}
		private int get_from_hours(TimeSpan time_span)
		{
			return time_span.Hours;
		}
		private int get_from_minutes(TimeSpan time_span)
		{
			return time_span.Minutes;
		}
		
	}
}
